#include <getopt.h>
#include <sstream>

#include "logger.h"
#include "socket_runner_builder.h"
#include "socket_runner.h"

int main(int argc, char **argv)
{
    int port = 2222;
    std::stringstream ss;
    while (true) {
        int optidx = 0;
        struct option options[] = {
            {"port", required_argument, 0, 'p'},
            {0,      0,                 0,  0 },
        };

        int c = getopt_long(argc, argv, "p:", options, &optidx);
        if (c == -1) {
            break;
        }

        switch (c) {
            case 'p':
                ss.clear();
                ss << optarg;
                ss >> port;
                if (!ss) {
                    return 1;
                }
                break;

            default:
                LOGGER() << "getopt_long: " << c << std::endl;
                break;
        }
    }

    SocketRunnerBuilder builder;
    builder.SetPort(port);
    auto runner = builder.Build();
    if (runner == nullptr) {
        return 1;
    }

    runner->Run();
    return 0;
}
